﻿using UnityEngine;
using System.Collections.Generic;

public class ObstacleFactory
{
    private ObstacleManager obstacleManager;

    public ObstacleFactory(ObstacleManager _obstacleManager)
    {
        obstacleManager = _obstacleManager;
    }

    public IList<Obstacle> createObstacles()
    {
        int countObstacles = obstacleManager.getCountObstacles();
        IList<Obstacle> result = createObstacles(countObstacles);

        return result;
    }

    private IList<Obstacle> createObstacles(int countObstacles)
    {
        IList<Obstacle> containerObstacles = new List<Obstacle>();

        for (int i = 0; i < countObstacles; i++)
        {
            Obstacle obstacle = createObstacle();
            addObstacleToContainer(obstacle, containerObstacles);
        }

        return containerObstacles;
    }

    private Obstacle createObstacle()
    {
        GameObject gameObjectObstacle = obstacleManager.createGameObjectObstacle();
        setUpPositionObstacle(gameObjectObstacle);
        
        Obstacle result = convertToObstacle(gameObjectObstacle);
        inactivateObstacle(result);

        return result;
    }

    private void setUpPositionObstacle(GameObject obstacle)
    {
        obstacle.transform.parent = obstacleManager.transform;
    }

    private Obstacle convertToObstacle(GameObject gameObject)
    {
        return gameObject.GetComponent<Obstacle>();
    }

    private void inactivateObstacle(Obstacle obstacle)
    {
        obstacle.inactive();
    }

    private void addObstacleToContainer(Obstacle obstacle, IList<Obstacle> obstacles)
    {
        obstacles.Add(obstacle);
    }
}